﻿import { _decorator, Component, Node, Quat, Vec3, v3, RigidBodyComponent } from "cc";

const { ccclass, property } = _decorator;

@ccclass("MapNavAgent")
export default class MapNavAgent extends Component {
    
    private speed: number = 0;
    private roadData: Array<any> = null;

    private nextStep: number = 1; // 
    private vx: number;
    private vy: number;
    private vz: number;

    private walkTime: number = 0.0;
    private passedTime: number = 0.0;
    private isWalking: boolean = false;

    private dstRot: Quat = null;
    private now: Quat = new Quat();

    private fun:Function=null


    onLoad(){
   

    }
    
    
    public InitMapNavAgent(speed: number): void {
        this.speed = speed;
    }

    public NavOnRoad(roadData: Array<any>, fun?:Function): void {
        this.fun=null
        if(fun){
            
            this.fun=fun
        }
        this.roadData = roadData;

        this.isWalking = false;
        if (this.roadData == null || this.roadData.length < 1) {
            
            return;
        }
        
        this.nextStep = 0;
        this.MoveToNext();
    }

    private MoveToNext(): void { // 从当前点---->this.roadData[this.nextStep]
        if (this.nextStep >= this.roadData.length) { // 走完了;
            
            
            this.isWalking = false;
            if(this.fun!=null){
              
             this.fun()
              
              
            }
         
            return;
        }

      
        this.isWalking = true;
        
        var src: Vec3 = this.node.getWorldPosition();
        var dst: Vec3 = v3(this.roadData[this.nextStep].x, this.roadData[this.nextStep].y, this.roadData[this.nextStep].z);

        var dir: Vec3 = v3(0, 0, 0);

        Vec3.subtract(dir, dst, src);
        var len = Vec3.len(dir);
        this.walkTime = len / this.speed;
        this.passedTime = 0;

this.vx = this.speed * dir.x / len;
        this.vy = this.speed * dir.y / len;
        this.vz = this.speed * dir.z / len;
  var old: Quat = this.node.getRotation();
        this.node.lookAt(dst); // 当前得方向 ----> 目标方向;
        this.dstRot = this.node.getRotation();
        this.node.setRotation(old);
    
        


    }

    public StopNav(): void {
        this.isWalking = false;
    }

	public update(dt: number): void {
        if (!this.isWalking) {
            
            return;
          
            
        }
        
        this.passedTime += dt;
        if (this.passedTime > this.walkTime) { 
            dt -= (this.passedTime - this.walkTime);
        }

        var pos = this.node.getWorldPosition();
        pos.x += (this.vx * dt);
        pos.y += (this.vy * dt);
        pos.z += (this.vz * dt);
        if (isNaN(pos.x) || isNaN(pos.y) || isNaN(pos.z)) {
              // 坐标包含 NaN
              this.StopNav()
              console.log("坐标包含 NaN",pos,this.node.name);
              return
          }
        this.node.setWorldPosition(pos);


        // 四元数得球面插值;
        Quat.slerp(this.now, this.node.getRotation(), this.dstRot, 10.0 * dt);
        this.node.setRotation(this.now);
        // end 

        if (this.passedTime >= this.walkTime) {
            this.nextStep ++;
            this.MoveToNext();
        }
	}
}
